import math

def Csum(num1, num2):
    return num1 + num2

def Csub(num1, num2):
    return num1 - num2

def Cmulti(num1, num2):
    return num1 * num2

def CDiv(num1, num2):
    if num2 == 0:
        print("除数不能为0")
        return None  # 返回 None 表示错误
    return num1 / num2

def logab(a, b):
    if a <= 0 or b <= 0 or b == 1:
        raise ValueError("对数运算要求底数大于0且不等于1，真数大于0")
    return math.log(a, b)

def power(num1, num2):
    return num1 ** num2

def modulus(num1, num2):
    if num2 == 0:
        print("除数不能为0")
        return None
    return num1 % num2

def get_number(choice_type):
    while True:
        try:
            if choice_type == "1":  # 复数
                num = complex(input("请输入一个数（格式如3+4j）："))
            else:  # 整数
                num = int(input("请输入一个数："))
            return num
        except ValueError:
            print("输入无效，请重新输入！")

def get_operator():
    valid_operators = ["+", "-", "*", "/", "log", "**", "%"]
    while True:
        operator = input("请输入运算符（+-*/ log ** %）：").strip()
        if operator in valid_operators:
            return operator
        else:
            print("输入的运算符无效，请重新输入！")

flag = True
while flag:
    choice_type = input("请问：需要复数(1)还是整数(2)运算？\n")
    if choice_type not in ["1", "2"]:
        print("输入有误，请重新输入！")
        continue

    num1 = get_number(choice_type)
    num2 = get_number(choice_type)

    operator = get_operator()

    if operator == "+":
        result = Csum(num1, num2)
        print(f"{num1} + {num2} = {result}")
    elif operator == "-":
        result = Csub(num1, num2)
        print(f"{num1} - {num2} = {result}")
    elif operator == "*":
        result = Cmulti(num1, num2)
        print(f"{num1} * {num2} = {result}")
    elif operator == "/":
        result = CDiv(num1, num2)
        if result is not None:
            print(f"{num1} / {num2} = {result}")
    elif operator == "log":
        try:
            result = logab(num1, num2)
            print(f"log以 {num2} 为底 {num1} 的对数 = {result}")
        except ValueError as e:
            print(f"对数运算出错：{e}")
    elif operator == "**":
        result = power(num1, num2)
        print(f"{num1} ** {num2} = {result}")
    elif operator == "%":
        result = modulus(num1, num2)
        if result is not None:
            print(f"{num1} % {num2} = {result}")

    conti = input("是否继续？Y/N\n")
    if conti.lower() != "y":
        flag = False